<?php
session_start();
error_reporting(1);
if(
    isset ( $_POST['offset'])
    && isset ( $_POST['number'])
)
{
    if ( isset ( $_SESSION['authenticated'] ) )
    {
        if( isset( $_POST ) && $_SERVER['REQUEST_METHOD'] == "POST" )
        {

            /*
          * ---------------------------------------
          *   Valid $offset && Valid $postnumbers
          * ---------------------------------------
          */
            $offset                 = is_numeric($_POST['offset']) ? $_POST['offset'] : die();
            $postnumbers            = is_numeric($_POST['number']) ? $_POST['number'] : die();

            /*
             * ---------------------------------------
             *   Query > ID || Query < ID
             * ---------------------------------------
             */
            if( $_POST['query'] == 1 )
            {
                $query = '<';
            }
            else
            {
                $query = '>';
            }

            /*
             * --------------------------
             *   Require/Include Files
             * -------------------------
             */
            require_once('../../class_ajax_request/classAjax.php');
            include_once('../../application/functions.php');
            include_once('../../application/DataConfig.php');


            /*
             * ----------------------
             *   Instance Class
             * ----------------------
             */
            $obj              = new AjaxRequest();
            if(isset($_POST['category']) && $_POST['category'] != 0){
                $str_category = '';
                ///$str_category =' && P.category_id = '.$_POST['category'];
                $cat = $obj->getcategory($_POST['category']);
                $cat = explode(';',$cat[0]['hash_tag']);
                foreach($cat as $item){
                    if(trim($item)!= ''){
                        if($str_category == '')
                            $str_category .= ' AND ( P.post_details LIKE "%'.trim($item).'%"';
                        else
                            $str_category .= ' OR  P.post_details LIKE "%'.trim($item).'%"';
                    }

                }
                $str_category .= ' ) ';


            } else{
                $str_category = '';
            }

            $str = 'WHERE U.status = "active"
		   && P.status = "1"
		   && B.id IS NULL
		   && U.mode = "1"
		   && P.id '.$query.' '.$offset .'
		   && P.repost_of_id = "0"
		   && F.id IS NULL
           '.$str_category;



            // tab friends
            if($_POST['query'] == 'friend'){
                $offset                 = is_numeric($_POST['offset']) ? $_POST['offset'] : die('offset');
                $postnumbers            = is_numeric($_POST['number']) ? $_POST['number'] : die('post number');
                $id_user                = is_numeric($_POST['_userId']) ? $_POST['_userId'] : $_SESSION['authenticated'];

                /*
                 * ---------------------------------------
                 *   Query > ID || Query < ID
                 * ---------------------------------------
                 */
                if( $_POST['query'] == 1 ) {
                    $query = '<';
                } else {
                    $query = '>';
                }

                if( !$_SESSION['authenticated'] ) {
                    $id_user_favs = 0;
                } else {
                    $id_user_favs = $_SESSION['authenticated'];
                }
                $response         = $obj->getFollowing(
                    'WHERE F.follower = '. $id_user .' && F.id '.$query.' '.$offset .'', 'LIMIT '.$postnumbers, $id_user_favs );
                $checkFollow       = $obj->checkFollow( $_SESSION['authenticated'], $id_user );

                $followId = array();
                if(!empty($response)){
                    foreach($response as $responseKey=> $responseVal){
                        $followId[] =$responseVal['id'];
                    }
                }

                if(count($followId) >0){
                    $where = ' WHERE ';
                    $i = 0;
                    foreach($followId as $id){
                        $i++;
                        $ext = " ";
                        if($i > 1){
                            $ext = " OR ";
                        }
                        $where .= $ext."  P.user = '".$id."'  ";

                    }

                } else{
                    $where .= " WHERE P.user = 0 ";
                }
                $postnumbers = 30;

                $str = $where;


            }



            //tap top
            if( isset($_POST['query']) && $_POST['query'] == 'top' ){


                $offset                 = is_numeric($_POST['offset']) ? $_POST['offset'] : die('offset');
                $postnumbers            = is_numeric($_POST['number']) ? $_POST['number'] : die('post number');
                $id_user                = is_numeric($_POST['_userId']) ? $_POST['_userId'] : $_SESSION['authenticated'];

                /*
                 * ---------------------------------------
                 *   Query > ID || Query < ID
                 * ---------------------------------------
                 */



                $str = 'WHERE U.status = "active" && P.status = "1" && B.id IS NULL && U.mode = "1" && P.id > 0 && P.repost_of_id = "0" && P.top = 1';

            }else{

            }

            $str .= ' GROUP BY P.id ORDER BY P.id DESC';

//            echo $str;

            if(($_POST['query'] == 'ask')){
                if(isset($_POST['ask'])){
                    $str = " WHERE P.id = ".$_POST['ask'];
                    $postnumbers = 1;

                }
            }


            $response         = $obj->discover(
                $str,
                'LIMIT '.$postnumbers,
                $_SESSION['authenticated'] );
            //echo $response;die;
            ?>
            <?php $countPosts = count( $response );
            if( $countPosts != 0 ) :
                foreach ( $response as $key ) {

                    $idPost  = $key['id'];
                    $_idUser = $key['user_id'];
                    $token_id = $key['token_id'];

                    if( $key['video_site'] != '' && $key['photo'] == '' ) {
                        $typeMedia            = $_SESSION['LANG']['video'];
                        $icon                 = '<i class="video_img_sm icons"></i>';
                    } else if( $key['video_site'] == '' && $key['photo'] != '' ) {
                        $typeMedia            = $_SESSION['LANG']['image'];
                        $icon                 = '<i class="ico_img_sm icons"></i>';
                    } else if( $key['url_soundcloud'] != '' ) {
                        $typeMedia             = null;
                        $icon                  = '<i class="icon_song_min icons"></i>';

                    } else  {
                        $typeMedia            = null;
                        $icon                 = null;
                    }
                    //============ VERIFIED
                    if( $key['type_account'] == '1' ) {
                        $verified = ' <img class="verified_img" src="'.URL_BASE.'public/img/verified_min.png">';
                    } else {
                        $verified = null;
                    }
                    //============ FAVORITES
                    if( $key['favoriteUser'] == 1 ) {
                        $iconFav      = ' iconfavorited';
                        $spanFav      = ' class="favorited" title="'.$_SESSION['LANG']['trash'].' '.$_SESSION['LANG']['favorite'].'"';
                        $spanAbsolute = '<span class="add_fav"></span>';
                        $textFav      = $_SESSION['LANG']['favorited'];
                    } else {
                        $iconFav      = null;
                        $spanFav      = null;
                        $spanAbsolute = null;
                        $textFav      = $_SESSION['LANG']['favorite'];
                    }

                    /*
                     * -------------------------------------------------
                     *      If the picture is larger than 440 pixels,
                     *      show the thumbnail
                     * -------------------------------------------------
                     */
                    $widthPhoto = _Function::getWidth( '../../upload/'.$key['photo'] );

                    if( $widthPhoto > 440 ) {
                        $thumbPic = 'thumb/450-450-';
                    } else  {
                        $thumbPic = null;
                    }

                    /* Url */
                    $urlStatus = URL_BASE.$key['username'].'/status/'.$key['id'];

                    $activeRepost = $obj->checkRepost( $key['id'], $_SESSION['authenticated'] );

                    //============ REPOST SESSION CURRENT
                    if( $activeRepost == 1  ) {
                        $iconRepost   = ' iconRepost';
                        $spanRepost   = ' class="repostedSpan"';
                        $textRepost   = $_SESSION['LANG']['reposted'];
                    } else  {
                        $iconRepost   = null;
                        $spanRepost   = null;
                        $textRepost   = $_SESSION['LANG']['repost'];
                    }

                    /*
                     * -------------------------------------
                     *  POST DETAILS / EXPAND / FAVS ETC
                     * -------------------------------------
                     */
                    //post meta
                    $disable = false;
                    $post_meta_array = $obj->get_post_meta($idPost);
                    $getMedia       = $obj->getMedia( $idPost, $token_id );
                    $votersCount = count($obj->getVoter($idPost));
                    $count_answer = $obj->gettanswerCount($idPost);
                    $countFavorite = $obj->favoriteCount($idPost);
                    $countReply = $obj->countReply($idPost);
                    $userInfoCus = $obj->infoUserLive($_SESSION['authenticated']);
                    $checkUseRelayPost = $obj->checkUserReplay($_SESSION['authenticated'],$idPost);
//                    echo '<pre>';
//                    print_r($checkUseRelayPost);
//                    echo '</pre>';
                    // include('voter.ajax.php');
                    //end post meta
                    if($checkUseRelayPost[0]['count_check'] == 0){
                        include('post_details.php');
                    }


                }//<--- Foreach
                die;
            endif; //<--- != 0
        }//<-- SESSION
    }//<-- if token id
}//<-- ISSET
?>